﻿using System;
using System.IO;
using HossMvp.Core;

namespace Sample.Models
{
    public class DocumentModel : AbstractModel
    {
        public DocumentModel(AbstractModelManager manager, string name, IContentProvider contents)
            : base(manager, name, contents)
        {
        }

        #region Content Provider -- Actions & Properties

        public class DocumentContentProvider : AbstractContentProvider
        {
            private string filename;
            private string contents;

            public DocumentContentProvider(string filename, NameDecorator decorator)
                : base(decorator)
            {
                this.filename = filename;
                //this.RegisterAction("LoadFile", this.LoadFile);
                //this.RegisterAction("SaveFile", this.SaveFile);
                //this.RegisterAction("PostClose", this.PostClose);
                this.RegisterActions();
            }

            private ActionResult LoadFile(ActionContext context)
            {
                using (var fileStream = new FileStream(this.filename, FileMode.OpenOrCreate))
                {
                    var reader = new StreamReader(fileStream);
                    this.contents = reader.ReadToEnd();
                    var result = new ActionResult()
                        .WithProperty("filename", this.filename)
                        .WithProperty("contents", this.contents);

                    return result;
                }
            }

            private ActionResult SaveFile(ActionContext context)
            {
                this.filename = context.Get<string>("filename");
                this.contents = context.Get<string>("contents");
                using (var fileStream = new FileStream(this.filename, FileMode.OpenOrCreate))
                {
                    var writer = new StreamWriter(fileStream);
                    writer.Write(this.contents);
                    var result = new ActionResult()
                        .WithProperty("filename", this.filename)
                        .WithProperty("contents", this.contents);
                    return result;
                }
            }

            private ActionResult PostClose(ActionContext arg)
            {
                return null;
            }

            public override T Get<T>(string name)
            {
                switch (name)
                {
                    case "contents":
                        return (T)Convert.ChangeType(this.contents, typeof(T));

                    case "filename":
                        return (T)Convert.ChangeType(this.filename, typeof(T));
                }

                return default(T);
            }

            public override void Set<T>(string name, T value)
            {
                throw new NotImplementedException();
            }
        }

        #endregion Content Provider -- Actions & Properties
    }
}